package com.iflytek.dao.impl;

import com.iflytek.bean.Emp;
import com.iflytek.dao.EmpDao;
import com.iflytek.util.Dbutils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
/*
 *员工数据库操作实现类
 */
public class EmpDaoImpl implements EmpDao {
    @Override
    public List<Emp> getAllEmp() {
        QueryRunner queryRunner = new QueryRunner(Dbutils.getDataSource());
        List<Emp> empList = null;

        try {
            empList = queryRunner.query("select * from emp", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return empList;
    }

    @Override
    public List<Emp>addEmp(Emp emp) {
        QueryRunner queryRunner = new QueryRunner(Dbutils.getDataSource());
        List<Emp> emp1 = null;
        try {
          emp1 = queryRunner.insert("insert into emp values(?,?,?,?,?,?,?) ",
                    new BeanListHandler<Emp>(Emp.class), "1020", "云飞", "学生", "1000000", "2021-03-16", "7000", "101");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return emp1;
    }

    @Override
    public void upDate(int empno) {
        QueryRunner queryRunner = new QueryRunner(Dbutils.getDataSource());
        try {
            int emp2 = queryRunner.update("update emp set ename=? where empno=?", "常云飞", "1017");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("修改成功！");

    }

    @Override
    public void delEmpById(int empno) {
        QueryRunner queryRunner = new QueryRunner(Dbutils.getDataSource());
        try {
            queryRunner.update("delete from emp where empno=?","1020");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("删除成功请查看:");
    }

    @Override
    public List<Emp> selectLikeName(String ename) {
        QueryRunner queryRunner = new QueryRunner(Dbutils.getDataSource());
        List<Emp> empList3 =null;
        try {
            empList3 = queryRunner.query("select * from emp where ename like '%王%'",new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return empList3;
    }

}


